﻿//-------------------------------------------------------------------------------------------------
// <copyright file="YahooHistoricalPricesUrl.cs" company="Andrew Craig Keller">
//  Copyright (c) Andrew Craig Keller.  All rights reserved.
// </copyright>
//-------------------------------------------------------------------------------------------------

namespace StockQuoteServices
{
    using System;

    /// <summary>
    /// Constructs a URL that can be used to retrieve historical prices for a stock from Yahoo's
    /// CSV API.
    /// </summary>
    internal class YahooHistoricalPricesUrl
    {
        /// <summary>
        /// The base url to be used for all requests.
        /// </summary>
        private const string BaseUrl = "http://ichart.yahoo.com/table.csv?s=";

        /// <summary>
        /// The ticker symbol of the stock for which data is to be retrieved.
        /// </summary>
        private string tickerSymbol;

        /// <summary>
        /// The start date of the range for which price history is to be retrieved.
        /// </summary>
        private DateTime priceRangeStart;

        /// <summary>
        /// The end date of the range for which price history is to be retrieved.
        /// </summary>
        private DateTime priceRangeEnd;

        /// <summary>
        /// Initializes a new instance of the <see cref="YahooHistoricalPricesUrl"/> class to be
        /// used to retrieve data between the given date range for the stock with the given ticker
        /// symbol.
        /// </summary>
        /// <param name="tickerSymbol">
        /// the ticker symbol of the stock for which data is to be retrieved
        /// </param>
        /// <param name="priceRangeStart">
        /// The starting date of the date range for which historical price data is to be retrieved
        /// </param>
        /// <param name="priceRangeEnd">
        /// The ending date of the date range for which historical price data is to be retrieved
        /// </param>
        public YahooHistoricalPricesUrl(
            string tickerSymbol,
            DateTime priceRangeStart,
            DateTime priceRangeEnd)
        {
            this.tickerSymbol = tickerSymbol;
            this.priceRangeStart = priceRangeStart;
            this.priceRangeEnd = priceRangeEnd;
        }

        /// <summary>
        /// Prevents a default instance of the <see cref="YahooHistoricalPricesUrl"/> class from
        /// being created.
        /// </summary>
        private YahooHistoricalPricesUrl()
        {
        }

        /// <summary>
        /// Gets the string representing of the URL to the resource providing historical stock
        /// price data.
        /// </summary>
        /// <returns>
        /// the string representation of the URL to the resource providing historical stock price
        /// data
        /// </returns>
        public override string ToString()
        {
            return BaseUrl
                + this.tickerSymbol
                + "&a="
                + (this.priceRangeStart.Month - 1)
                + "&b="
                + this.priceRangeStart.Day
                + "&c="
                + this.priceRangeStart.Year
                + "&d="
                + (this.priceRangeEnd.Month - 1)
                + "&e="
                + this.priceRangeEnd.Day
                + "&f="
                + this.priceRangeEnd.Year
                + "&g=d&ignore=.csv";
        }
    }
}
